// miniprogram/pages/delivery_state/delivery_state.js
const db = require('../../utils/db');
const datetime = require('../../utils/datetime');

let id;

Page({
  data: {
    busy: {
      type: Boolean,
      value: true
    },
    found: {
      type: Boolean,
      value: false
    },
    delivery: {
      type: Object,
      value: {}
    }
  },

  onLoad: function (options) {
    id = options.id;
  },

  onReady: function () {
    db.getDelivery(id).then(out => {
      console.log(out);
      if (out.code == 0) {
        switch (out.result.state) {
          case 'new':
            out.result['status'] = '已提交';
            break;
          case 'processing':
            out.result['status'] = '处理中';
            break;
          case 'closed':
            out.result['status'] = '已完成';
            break;
          default:
            break;
        }
        out.result.time = datetime.toFriendlyTime(out.result.time);
        out.result._type = out.result._type === 'borrow' ? '借' : '还';
        this.setData({
          busy: false,
          found: true,
          delivery: out.result
        });
      } else {
        this.setData({
          busy: false,
          found: false
        });
      }
    });
  },

  onShow: function () {

  },

  onHide: function () {

  },

  onUnload: function () {

  },

  onPullDownRefresh: function () {

  }
})
